﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationTest
{
    class HttpUtil
    {
        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

        public static string Post(string requestUrl, string message, Encoding encoding,
            int timeout, IWebProxy webProxy, bool checkValidate = true)
        {
            string responseMessage = "";

            //设置https验证方式
            if (checkValidate && requestUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                // ServicePointManager.DefaultConnectionLimit = 1000;
                // ServicePointManager.MaxServicePointIdleTime = 4000;
                // ServicePointManager.MaxServicePointIdleTime = 2;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Console.WriteLine("inline");
            }
            Console.WriteLine("Step1...");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
            System.Net.Http.HttpClient dd = new HttpClient();

            if (webProxy != null)
            {
                request.Proxy = webProxy;
            }
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout = timeout;
            request.ReadWriteTimeout = timeout;
            request.ServicePoint.ConnectionLimit = 4000;

            Byte[] bysMessage = encoding.GetBytes(message);
            request.ContentLength = bysMessage.Length;

            Console.WriteLine("Step2...");

            Console.WriteLine("timeout: " + request.Timeout);

            Stopwatch watch = new Stopwatch();
            watch.Start();

            try
            {
                using (Stream writer = request.GetRequestStream())
                {
                    Console.WriteLine("After GetRequestStream()");
                    writer.Write(bysMessage, 0, bysMessage.Length);
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("timespan2:" + watch.ElapsedMilliseconds.ToString("N0") + "ms");

            Console.WriteLine("Step3...");

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (Stream sr = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(sr, encoding))
                    {
                        responseMessage = reader.ReadToEnd();
                    }
                }
            }

            Console.WriteLine("timespan3:" + watch.ElapsedMilliseconds.ToString("N0") + "ms");

            return responseMessage;
        }
    }
}
